package form.ui
{
	import flash.filters.DropShadowFilter;
	
	import form.ui.core.Arrow;
	import form.ui.core.FMSprite;

	public class ArrowLayer extends FMSprite
	{
		public static const MIN_WIDTH:uint = 40;
		public static const MIN_HEIGHT:uint = 20;
		
		public function ArrowLayer()
		{
			super();	
			this.fillColor = 0xFFFFFF;
			this.strokeColor = 0x0099FF;		
			this.strokeThickness = 1;
			this.fillOpacity = 1;
			this.strokeOpacity = 1;
			
			this.showShadow = true;
			
			this.width = MIN_WIDTH;
			this.height = MIN_HEIGHT;
		}
		
		private var _arrow:String = "up";
		public function get arrow():String
		{
			return _arrow;
		}		
		public function set arrow(value:String):void
		{
			if(_arrow != value)
			{
				_arrow = value;
				draw();
			}
			
		}
		
		private var _arrowSize:Number = 10;
		public function get arrowSize():Number
		{
			return _arrowSize;
		}
		public function set arrowSize(value:Number):void
		{
			if(_arrowSize != value)
			{
				_arrowSize = value;
				draw();
			}
			
		}
		
		private var shadow:DropShadowFilter = new DropShadowFilter(4,45,0x0);
		private var _showShadow:Boolean;
		public function get showShadow():Boolean
		{
			return _showShadow;
		}
		public function set showShadow(value:Boolean):void
		{
			if(_showShadow != value)
			{
				var a:Array = this.filters = [];
				if(a && a.indexOf(shadow) != -1)
				{
					a.splice(a.indexOf(shadow), 1);
				}	
				_showShadow = value;
				if(_showShadow)a.push(shadow);
				this.filters = a;
			}
		}
		
		//draw an arrow-based rectangle
		//                       (_x2,_y2) 
		//              L (_x1,_y1)/ \(_x3,_y3) R
		//  (x1,y1) -------------------------------(x2, y2)
		//    |                                           |
		//  L |                                           |L
		//  R |                                           |R
		//    |                                           |
		//   (x4, y4) ---------------------------- (x3, y3)
		//                      L    R 
		override public function draw():void
		{
			if(!this.width || !this.height || this.width < MIN_WIDTH || this.height < MIN_HEIGHT)return;
			if(this.fillOpacity == 0 || this.strokeOpacity == 0)
			{
				super.draw();
				return;
			}
			var arrowWidth:Number = this.arrowSize * 0.7;
			var xa:Array/* of Number */ = [], ya:Array/* of Number */ = [];
			var x1:Number, y1:Number, x2:Number, y2:Number, x3:Number, y3:Number, x4:Number, y4:Number;
			var _x1:Number, _y1:Number, _x2:Number, _y2:Number, _x3:Number, _y3:Number;
			switch(this.arrow)
			{
				case Arrow.UP:					
					x1 = 0; y1 = arrowSize;
					x2 = this.width; y2 = arrowSize;
					x3 = this.width; y3 = this.height + arrowSize;
					x4 = 0; y4 = this.height + arrowSize;
					_x2 = this.width / 2; _y2 = 0;
					_x1 = _x2 - arrowWidth; _y1 = arrowSize;
					_x3 = _x2 + arrowWidth; _y3 = arrowSize;
					xa = [x1, _x1, _x2, _x3, x2, x3, x4, x1];
					ya = [y1, _y1, _y2, _y3, y2, y3, y4, y1];
					break;
				case Arrow.DOWN:
					x1 = 0; y1 = 0;
					x2 = this.width; y2 = 0;
					x3 = this.width; y3 = this.height;
					x4 = 0; y4 = this.height;
					_x2 = this.width / 2; _y2 = this.height + arrowSize;
					_x1 = _x2 - arrowWidth; _y1 = this.height;
					_x3 = _x2 + arrowWidth; _y3 = this.height;
					xa = [x1, x2, x3, _x3, _x2, _x1, x4, x1];
					ya = [y1, y2, y3, _y3, _y2, _y1, y4, y1];
					break;
				case Arrow.LEFT:
					x1 = arrowSize; y1 = 0;
					x2 = this.width + arrowSize; y2 = 0;
					x3 = this.width + arrowSize; y3 = this.height;
					x4 = arrowSize; y4 = this.height;
					_x2 = 0; _y2 = this.height/2;
					_x1 = arrowSize; _y1 = this.height / 2 - arrowWidth;
					_x3 = arrowSize; _y3 = this.height / 2 + arrowWidth;
					xa = [x1, x2, x3, x4, _x3, _x2, _x1, x1];
					ya = [y1, y2, y3, y4, _y3, _y2, _y1, y1];
					break;
				case Arrow.RIGHT:
					x1 = 0; y1 = 0;
					x2 = this.width ; y2 = 0;
					x3 = this.width; y3 = this.height;
					x4 = 0; y4 = this.height;
					_x2 = this.width + arrowSize; _y2 = this.height/2;
					_x1 = this.width; _y1 = this.height / 2 - arrowWidth;
					_x3 = this.width; _y3 = this.height / 2 + arrowWidth;
					xa = [x1, x2, _x1, _x2, _x3, x3, x4, x1];
					ya = [y1, y2, _y1, _y2, _y3, y3, y4, y1];
					
			}
			this.graphics.clear();
			
			this.graphics.beginFill(this.fillColor, this.fillOpacity);
			this.graphics.lineStyle(1,0);
			this.graphics.moveTo(x1, y1);
			if (this.strokeThickness && this.strokeOpacity) 
			{
				this.graphics.lineStyle(this.strokeThickness, this.strokeColor, this.strokeOpacity);
			}
			var i:int = 0, n:uint = xa.length;
			for(i = 1; i < n; i ++)
			{
				this.graphics.lineTo(xa[i], ya[i]);
			} 			
			this.graphics.endFill();
		}
	}
}
